package com.zx.flow.core.execute.eventhandler;

import com.zx.flow.api.dict.FlowDict.Status;
import com.zx.flow.api.execute.model.FlowContext;
import com.zx.flow.api.execute.request.FlowParamSave;
import com.zx.flow.api.execute.response.FlowResult;
import com.zx.flow.api.util.AssertUtil;
import com.zx.flow.core.execute.EventHandler;
import java.util.Objects;

/**
 * 事件处理器【保存流程】
 */
public class EventFlowSaveHandler extends EventHandler<FlowParamSave> {

    @Override
    public boolean in(FlowContext flow, FlowParamSave param) {
        AssertUtil.checkApplyAccount(flow, param.getAccountCode());
        return flow != null;
    }

    @Override
    public FlowResult on(FlowContext flow, FlowParamSave param) {
        if (Objects.equals(flow.getStatus(), Status.flowNew)) {
            flow.setStatus(Status.flowSave);
        }
        // 保存流程数据
        this.saveFlow(flow);
        return FlowResult.ok(flow);
    }
}
